package com.nowoe.server.util;

import com.nowoe.server.constant.Http;

/**
 * @author 倚栏听风 2024/1/5 14:50
 */
public class HttpProtocolUtil {

    public static String getHttpHeader(int status, long contentLength) {
        if (status == Http.SUCCESS) {
            return "HTTP/1.1 200 OK\n" +
                    "Content-Type: text/html;charset=UTF-8\n" +
                    "Content-Length: " + contentLength + "\n" +
                    "\n";
        } else if (status == Http.NOT_FOUND) {
            String title = "<h1>404 Not Found</h1>";
            return "HTTP/1.1 404 Not Found\n " +
                    "Content-Type: text/html;charset=UTF-8\n" +
                    "Content-Length: " + title.getBytes().length + "\n" +
                    "\n";
        } else if (status == Http.NOT_ALLOWED) {
            String title = "<h1>405 Method Not Allowed</h1>";
            return "HTTP/1.1 405 Not Allowed\n " +
                    "Content-Type: text/html;charset=UTF-8\n" +
                    "Content-Length: " + title.getBytes().length + "\n" +
                    "\n";
        }
        return "Failed !!!";

    }
}
